from flask import Blueprint, render_template, request, flash, abort
from app.models import Article, Category
from app.constants import PAGE_LIMIT

home_category = Blueprint("home_category", __name__)

@home_category.route("/index")
def index():
    page = request.args.get('page', 1, type=int)
    catid = request.args.get('catid', 1, type=int)
    
    try:
        category = Category.query.filter_by(id=catid).first()
        pagination = Article.query.filter_by(category_id=catid).order_by(Article.id.desc()).paginate(page = page, per_page=PAGE_LIMIT)
    except Exception as e:
        flash(str(e))
        abort(500)

    return render_template("home/category.html",pagination=pagination, category=category)